package com.midian.action.mc.common;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.midian.common.dao.OrderHelper;
import com.midian.common.exception.BizException;
import com.midian.common.pager.Pager;
import com.midian.common.system.SysStatic;
import com.midian.common.utils.FUtil;
import com.midian.model.common.BaseModel;
import com.midian.service.common.IBaseService;

/**
 * 
 * @Title:公告操作控制器  
 * @Desription:CommonAction 公告操作控制器，完成显示列表，添加，修改，删除等公共功能
 * @Company:MDKG
 * @ClassName:AjaxAction.java
 * @Author:Mengfh
 * @CreateDate:2013-6-7 下午5:37:25  
 * @UpdateUser:Mengfh  
 * @Version:0.1
 */
public abstract class AjaxAction<T extends BaseModel> extends BaseAction {
	
	private static final long serialVersionUID = -6074275002538269972L;
	/**业务*/
    protected IBaseService<T> service;
    /**获取查询对象*/
	protected abstract T dytEntity();
	/**设置查询对象*/
	protected abstract void dyEntity(T t);
	/**设置返回列表*/
	public abstract void setRows(List<T> rows);
	/**获取返回列表*/
	public abstract List<T> getRows();
	/**
	 * 
	 * @ClassName: AjaxAction.java
	 * @Description: 显示列表页面
	 * @Author:Mengfh
	 * @CreateDate:2013-6-7 下午5:37:46
	 * @return
	 */
	@SkipValidation
	public String ajax(){
		return SHOWLIST;
	}
	
	/**
	 * 获取结果集数据
	 * @param pager 分页对象
	 * @return
	 */
	private String getDataList(Pager pager) {
		try {
			List<OrderHelper> order = getOrder();
			setRows(service.showList(dytEntity(), pager, order));
			List<T> rows = getRows();
			if (!rows.isEmpty()) {
				setPager(rows.get(0).getPager());
			}
		} catch (BizException e) {
			getError(e);
		}
		return SUCCESS;
	}
	
	/**
	 * 分页显示结果集列表
	 * @return
	 */
	public String showList(){
		if(this.pager == null) {
			this.pager = new Pager();
		}
		return getDataList(this.pager);
	}
	
	/**
	 * 显示结果集列表
	 * @return
	 */
	public String showListWithoutPager() {
		return getDataList(null);
	}
	
	/**
	 * 
	 * @ClassName: AjaxAction.java
	 * @Description: 显示指定的详细信息
	 * @Author:Mengfh
	 * @CreateDate:2013-6-7 下午5:38:07
	 * @return
	 */
	@SkipValidation
	public String showInfo(){
		try {
			if(StringUtils.isNotBlank(this.id)) {
				dyEntity(service.get(this.id));
			}
		} catch (BizException e) {
			getError(e);
		}
		return SHOWINFO;
	}
	
	/**
	 * 
	 * @ClassName: AjaxAction.java
	 * @Description: 添加记录
	 * @Author:Mengfh
	 * @CreateDate:2013-6-7 下午5:38:17
	 * @return
	 */
	public String saveInfo(){
		try{
			String id = dytEntity().getId();
			if(StringUtils.isBlank(id)){
				service.add(dytEntity());
			}else{
				super.setId(id); //设置Action的id属性，flexigrid操作数据使用
				service.update(dytEntity());
			}
		}catch (BizException e) {
			getError(e);
		}catch (Exception e) {
			BizException be = new BizException("添加失败", e);
			getError(be);
		}
		return SAVEINFO;
	}
	
	/**
	 * 
	 * @ClassName: AjaxAction.java
	 * @Description: 删除记录
	 * @Author:Mengfh
	 * @CreateDate:2013-6-7 下午5:38:28
	 * @return
	 */
	public String delInfo(){
		try{
			service.delete(this.ids);
		}catch (BizException e) {
			getError(e);
		}
		return SUCCESS;
	}
	
	/**
	 * 禁用实体
	 * @return
	 */
	public String disableEntity() {
		try {
			service.opDelFlag(ids, SysStatic.DELFLAG);
		} catch (BizException e) {
			getError(e);
		}
		return SUCCESS;
	}
	
	/**
	 * 恢复实体
	 * @return
	 */
	public String ableEntity() {
		try {
			service.opDelFlag(ids, SysStatic.NORMALDELFLAG);
		} catch (BizException e) {
			getError(e);
		}
		return SUCCESS;
	}
	
	/**
	 * 下载文件
	 * @return
	 */
	public String download() {
		FUtil fUtil = new FUtil();
		String fileName = super.getDownloadFileName();
		if(fileName.indexOf(".") != -1) {
			fileName = fileName.substring(0, fileName.lastIndexOf("."));
		}
		super.setInputFile(fUtil.downloadFile(fileName));
		return DOWN;
	}
	
	/*public String updateGradation(){
		try {
			biz.updateGradation(ids, nums);
		} catch (BizException e) {
			//e.printStackTrace();
			getError(e);
		}
		return SUCCESS;
	}*/
}
